package com.example.webdownloadserver.handle;

import java.util.Map;

import com.example.webdownloadserver.base.TaskHandlerAbstract;
import com.example.webdownloadserver.base.TaskJobException;
import com.example.webdownloadserver.core.excel.ExcelExportIface;
import com.example.webdownloadserver.core.excel.TemplateExcelExport;
import com.example.webdownloadserver.service.TaskCenterService;
import com.example.webdownloadserver.util.JsonUtils;
import com.example.webdownloadserver.vo.TaskCenter;
import org.springframework.beans.factory.annotation.Autowired;

import com.dnkj.download.task.util.Sysdate;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * <p>
 * 下载任务公共逻辑抽象类
 * </p>
 * @title DownloadHandlerAbstract.java
 * @package com.dnkj.download.task.handle 
 * @author ying.xiao
 * @version 0.1 2020年12月5日
 */
@Slf4j
public class DownloadHandlerAbstract<T extends ExcelExportIface> extends TaskHandlerAbstract {
	@Autowired
	private TaskCenterService taskCenterService;
	
	public void abstractHandler(T service, Class<?> pojoClass, TaskCenter taskCenter, String eName, String cName, String path) {
		try {
            Long taskId = taskCenter.getTaskId();
            String jsonParam = taskCenter.getTaskParam();
            //TODO 兼容参数过长,考虑使用redis
            log.debug("Download{} start,json param is {}",eName,jsonParam);
            Map<String,Object> paramMap = JsonUtils.parser(jsonParam, Map.class);
            log.debug("Download{} start,json param map is {}",eName,jsonParam);
			TaskCenter bean = taskCenterService.get(taskId);
			String fileName = eName + "-" + Sysdate.getTimeStr(true)+"-"+ taskCenter.getTaskOperator()+"-"+(int)(Math.random()*900)+100;
			TemplateExcelExport template = new TemplateExcelExport(service, taskCenterService, bean, fileName);
			template.doExcelExport(pojoClass, paramMap);
		} catch (TaskJobException e) {
			throw e;
		} catch(Exception e) {
            log.error("{} Error",eName, e);
            throw new TaskJobException("{}下载异常", cName, e);
		}
	}
	
}
